package top.sdwu_top.kbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.sdwu_top.kbase.common.constants.Constants;
import top.sdwu_top.kbase.common.enums.ApplyStateEnums;
import top.sdwu_top.kbase.common.model.Wrapper;
import top.sdwu_top.kbase.entity.KbaseAuthUser;
import top.sdwu_top.kbase.entity.KbaseFile;
import top.sdwu_top.kbase.entity.KbaseFileVersion;
import top.sdwu_top.kbase.entity.KbaseFileVersionApply;
import top.sdwu_top.kbase.mapper.KbaseFileVersionApplyMapper;
import top.sdwu_top.kbase.service.IKbaseAuthUserService;
import top.sdwu_top.kbase.service.IKbaseFileService;
import top.sdwu_top.kbase.service.IKbaseFileVersionApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.sdwu_top.kbase.service.IKbaseFileVersionService;
import top.sdwu_top.kbase.vo.KbaseFileVersionApplyVo;

import java.util.Objects;

/**
 * <p>
 * 文档贡献申请 服务实现类
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-03-14
 */
@Service
public class KbaseFileVersionApplyServiceImpl extends ServiceImpl<KbaseFileVersionApplyMapper, KbaseFileVersionApply> implements IKbaseFileVersionApplyService {

    @Autowired
    IKbaseFileVersionService fileVersionService;

    @Autowired
    IKbaseFileService fileService;

    @Autowired
    IKbaseAuthUserService userService;

    @Override
    @Transactional
    public boolean pass(KbaseAuthUser user, String id) {
        KbaseFileVersionApply apply = super.getById(id);
        if (Objects.isNull(apply) || StringUtils.equalsIgnoreCase(apply.getState(), ApplyStateEnums.REFUSED.getKey())) {
            return false;
        }
        if (!fileService.isCreator(user, apply.getFileId())) {
            return false;
        }
        apply.setState(ApplyStateEnums.PASSED.getKey());
        boolean flag = super.updateById(apply);
        if (flag) {
            //修改文件的最新状态
            KbaseFile file = fileService.getById(apply.getFileId());
            file.setFileType(apply.getFileType());
            file.setFilePath(apply.getFileVersionPath());
            //追加一条文件历史记录
            KbaseFileVersion fileVersion = new KbaseFileVersion();
            fileVersion.setVersionBy(apply.getCreatedBy());
            fileVersion.setContent(apply.getContent());
            fileVersion.setFileId(apply.getFileId());
            fileVersion.setVersion(fileVersionService.getLastVersion(apply.getFileId()));
            fileVersionService.save(fileVersion);
        }
        return flag;
    }

    public boolean abort(KbaseAuthUser user, String id) {
        if (StringUtils.isBlank(id)) {
            return false;
        }
        KbaseFileVersionApply apply = super.getById(id);
        if (Objects.isNull(apply)) {
            return false;
        }
        if (!fileService.isCreator(user, apply.getFileId())) {
            return false;
        }
        apply.setState(ApplyStateEnums.REFUSED.getKey());
        return super.updateById(apply);
    }

    @Override
    public Page<KbaseFileVersionApply> page(KbaseAuthUser user, KbaseFileVersionApplyVo vo) {
        if (!fileService.isCreator(user, vo.getFileId()) || StringUtils.isBlank(vo.getFileId())) {
            return vo.getPage();
        }
        QueryWrapper<KbaseFileVersionApply> qw = Wrappers.query();
        qw.eq(StringUtils.isNotBlank(vo.getFileId()), "FILE_ID", vo.getFileId())
                .eq(StringUtils.isNotBlank(vo.getState()), "state", vo.getState());
        Page<KbaseFileVersionApply> page = super.page(vo.getPage(), qw);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            page.getRecords().parallelStream().forEach(i->{
                i.setStateName(ApplyStateEnums.getName(i.getState()));
                KbaseAuthUser createUser = userService.getById(i.getCreatedBy());
                if (Objects.nonNull(createUser)) {
                    i.setCreatedName(createUser.getSsn());
                }
            });
        }
        return page;
    }
}
